SPI to I2C Click
SPI to I2C Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.
Click Library
- Author : Stefan Filipovic
- Date : Jan 2023.
- Type : SPI type
Software Support
Example Description
This example demonstrates the use of SPI to I2C Click board by reading the manufacturer ID of a 3D Hall 11 Click board connected to the I2C port and controlling the GPIO pins.
Example Libraries
- MikroSDK.Board
- MikroSDK.Log
- Click.SPItoI2C
Example Key Functions
- spitoi2c_cfg_setup Config Object Initialization function.
void spitoi2c_cfg_setup(spitoi2c_cfg_t *cfg)
SPI to I2C configuration object setup function.
SPI to I2C Click configuration object.
Definition spitoi2c.h:225
- spitoi2c_init Initialization function.
err_t spitoi2c_init(spitoi2c_t *ctx, spitoi2c_cfg_t *cfg)
SPI to I2C initialization function.
SPI to I2C Click context object.
Definition spitoi2c.h:207
- spitoi2c_default_cfg Click Default Configuration function.
err_t spitoi2c_default_cfg(spitoi2c_t *ctx)
SPI to I2C default configuration function.
- spitoi2c_i2c_read_after_write This function performs a write then read with a repeated start to the I2C target device.
uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len );
err_t spitoi2c_i2c_read_after_write(spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len)
SPI to I2C i2c read after write function.
- spitoi2c_gpio_write This function writes a desired data to the gpio port.
err_t spitoi2c_gpio_write(spitoi2c_t *ctx, uint8_t gpio_data)
SPI to I2C gpio write function.
- spitoi2c_gpio_read This function reads data from the gpio port.
err_t spitoi2c_gpio_read(spitoi2c_t *ctx, uint8_t *gpio_data)
SPI to I2C gpio read function.
Application Init
Initializes the driver and performs the Click default config which enables the device and sets the GPIO pins 0-2 as push-pull output and others as input. Then sets the I2C clock to 99KHz, I2C address to 127 and disables I2C timeout. After that, reads and displays the chip firmware version.
{
log_cfg_t log_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
if ( DIGITAL_OUT_UNSUPPORTED_PIN ==
spitoi2c_init( &spitoi2c, &spitoi2c_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
{
log_error( &logger, " Default configuration." );
for ( ; ; );
}
uint8_t version[ 16 ] = { 0 };
{
log_printf( &logger, " Firmware version: %s\r\n", version );
}
log_info( &logger, " Application Task " );
}
#define SPITOI2C_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition spitoi2c.h:191
err_t spitoi2c_read_version(spitoi2c_t *ctx, uint8_t *version)
SPI to I2C read version function.
void application_init(void)
Definition main.c:40
@ SPITOI2C_OK
Definition spitoi2c.h:244
@ SPITOI2C_ERROR
Definition spitoi2c.h:245
Application Task
Reads the manufacturer ID of a 3D Hall 11 Click board connected to the I2C port, toggles the output pins and displays the GPIO port state. The results will be displayed on the USB UART approximately once per second.
{
uint8_t man_id[ 2 ] = { 0 };
®_addr, 1, man_id, 2 );
log_printf( &logger,
" %s - ID read: %s\r\n", (
char * )
DEVICE_NAME,
(
SPITOI2C_OK == error_flag ) ) ?
"Success" :
"Fail" ) );
{
log_printf( &logger, " GPIO state: 0x%.2X\r\n\n", ( uint16_t ) gpio_state );
gpio_state = ~gpio_state;
}
Delay_ms ( 1000 );
}
#define SPITOI2C_NO_PIN_MASK
SPI to I2C pin mask values.
Definition spitoi2c.h:137
void application_task(void)
Definition main.c:82
#define DEVICE_NAME
Definition main.c:31
#define DEVICE_MAN_ID_1
Definition main.c:35
#define DEVICE_REG_MAN_ID
Definition main.c:33
#define DEVICE_SLAVE_ADDRESS
Definition main.c:32
#define DEVICE_MAN_ID_0
Definition main.c:34
Application Output
This Click board can be interfaced and monitored in two ways:
- Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
- UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.
Additional Notes and Information
The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.